//
//  RegisterViewController.m
//  Molian
//
//  Created by 胡津 on 15/4/15.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "RegisterViewController.h"
#import "MLLoginTextField.h"
#import "VerificationnumberViewController.h"
#import "WEBViewController.h"

@interface RegisterViewController ()
@property (nonatomic, strong)MLLoginTextField *phonenumber;
@property (nonatomic, strong)MLLoginTextField *password;

@end

@implementation RegisterViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"注册";
    UIBarButtonItem *rightItem  = [[UIBarButtonItem alloc]initWithTitle:@"下一步" style:UIBarButtonItemStylePlain target:self action:@selector(rightItem)];
    self.navigationItem.rightBarButtonItem = rightItem;

    //手机号码文本框
    self.phonenumber = [[MLLoginTextField alloc]initWithFrame:CGRectMake(0, 80, DeviceWidth, 40)];
    self.phonenumber.placeholder = @"请输入手机号码";
    self.phonenumber.keyboardType = UIKeyboardTypePhonePad;
    self.phonenumber.leftPrompt = @"  手机号  ";
    [self.view addSubview:self.phonenumber];
    [self.phonenumber style];
    
    //第一次输入密码
    self.password = [[MLLoginTextField alloc]initWithFrame:CGRectMake(0, 130, DeviceWidth, 40)];
    self.password.placeholder = @"请设置6 ~ 16位的密码";
    self.password.secureTextEntry = YES;
    self.password.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.password.leftPrompt = @"  密码      ";
    [self.view addSubview:self.password];
    [self.password style];

    
    //密码提示
    UILabel *prompt = [[UILabel alloc]initWithFrame:CGRectMake(20, 190, 300, 20)];
    prompt.text = @"点击 \"下一步\" ,即表示同意";
    prompt.textColor = [UIColor grayColor];
    prompt.font = [UIFont fontWithName:FontName size:13];
    [self.view addSubview:prompt];
    
    //用户协议按钮
    UIButton *agreement = [UIButton buttonWithType:UIButtonTypeCustom];
    agreement.frame = CGRectMake([Utils LabeSizeText:prompt.text Font:prompt.font].width+20, 190, [Utils LabeSizeText:@"《口袋恋人用户许可协议》" Font:prompt.font].width, 20);
    agreement.titleLabel.textAlignment = NSTextAlignmentLeft;
    [agreement setTitle:@"《口袋恋人用户许可协议》" forState:UIControlStateNormal];
    [agreement addTarget:self action:@selector(permission) forControlEvents:UIControlEventTouchUpInside];
    [agreement setTitleColor:RGBA(130, 153, 190, 1) forState:UIControlStateNormal];
    agreement.titleLabel.font = [UIFont fontWithName:FontName size:13];
    [self.view addSubview:agreement];
    
    
    
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(returnKeyBorad)]];
    
}

//注册
-(void)rightItem
{
    // 获取输入的字符
    NSString *accountName = self.phonenumber.text;
    NSString *password = self.password.text;
    
    // 验证输入的参数
    BOOL isParamValid = [self verifyLoginParam:accountName password:password];
    if (!isParamValid) {
        return;
    }
    
    // 注册账号
    MPUser *user = [MPUser user];
    user.username = accountName;
    user.mobilePhoneNumber = accountName;
    user.password = password;
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        [HUD hideFromViewAnimated:YES];
        if (!error) {
            // 跳转到下一个页面
            VerificationnumberViewController *vc = [[VerificationnumberViewController alloc]init];
            vc.phoneNumber = accountName;
            vc.password = password;
            [self.navigationController pushViewController:vc animated:YES];
        }
        else {
            NSString *msg = @"注册失败,请重试";
            if (error.code == AVOS_RES_CODE_MOBILE_EXIST) {
                msg = @"已经为你创建账户，请直接登录验证手机号";
            }
            else if (error.code == AVOS_RES_CODE_MOBILE_INVALID) {
                msg = @"无效的手机号";
            }
            [HUD Prompt:msg ];
        }
    }];
    [HUD setOnViewWithTitle:nil animated:YES];
    
}

//输入判断
- (BOOL)verifyLoginParam:(NSString *)accountName password:(NSString *)password {
    if (!accountName || [accountName isEqualToString:@""]) {
        [HUD Prompt:@"请输入手机号"];
        return NO;
    } else if (!password || [password isEqualToString:@""]) {
        [HUD Prompt:@"请输入密码"];
        return NO;
    } else if (![self validatepassword:self.password.text]){
        [HUD Prompt:@"密码应该是6 ~ 16位数字和字母的组合"];
        return NO;
    }
    return YES;
}

//正则表达式:密码难度
-(BOOL) validatepassword:(NSString *)password
{
    NSString *pwdRegex = @"^[a-zA-Z0-9]{6,16}+$";
    NSPredicate *pwdTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pwdRegex];
    return [pwdTest evaluateWithObject:password];
}


//用户许可协议
-(void)permission
{
    WEBViewController *web = [[WEBViewController alloc]init];
    web.WEBURL = @"http://www.pocketsweet.com/protocol/";
    [self.navigationController pushViewController:web animated:YES];
}

//回收键盘
-(void)returnKeyBorad
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
